﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;

namespace NJL.WebManage.Company
{
    public partial class CompanyService : BasePage
    {
        NJL.BLL.NJL_COMPANY_SERVICE bll = new BLL.NJL_COMPANY_SERVICE();
        NJL.Model.NJL_COMPANY_SERVICE model = new Model.NJL_COMPANY_SERVICE();
        NJL.BLL.NJL_CS_SERVICE_TYPE bllServiceType = new BLL.NJL_CS_SERVICE_TYPE();
        NJL.BLL.NJL_IMAGE bllImage = new BLL.NJL_IMAGE();
        NJL.BLL.NJL_COMPANY bllCompany = new BLL.NJL_COMPANY();
        NJL.Model.NJL_COMPANY modelCompany;
        protected void Page_Load(object sender, EventArgs e)
        {
            modelCompany = bllCompany.GetModel(Request.QueryString["id"].ToString());
            if (!IsPostBack)
            {
                ViewState["state"] = "add";
                for (int i = 1; i <= 5; i++)
                {
                    radlLevel.Items.Add(new ListItem("" + i.ToString(), "" + i.ToString())); //服务等级
                }
                radlLevel.Items[0].Selected = true;
                bind();

            }
        }
        public void bind()
        {
            litCompanyName.Text = modelCompany.COMPANY_NAME;
            DataSet ds = bllServiceType.GetList("");
            radlServiceType.DataSource = ds;
            radlServiceType.DataTextField = "TYPE_NAME";
            radlServiceType.DataValueField = "TYPE_CODE";
            radlServiceType.DataBind();
            rptUserList.DataSource = bll.GetList("NJL_COMPANY_ID=" + Request.QueryString["id"]).Tables[0].DefaultView;
            rptUserList.DataBind();
        }
        protected void rptUserList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                string id = e.CommandArgument.ToString();
                model = bll.GetModel(id);
                if (model != null)
                {
                    DeleteImageByCode(model.IMAGE_CODE);   //删除文件

                }
                if (bll.Delete(id))
                {
                    bind();
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "删除失败");
                }
            }
            else if (e.CommandName == "Edit")
            {
                string id = e.CommandArgument.ToString();
                model = bll.GetModel(id);
                if (model != null)
                {
                    Button1.Visible = true;
                    txtServiceName.Text = model.SERVICE_NAME;
                    txtServiceTag.Text = model.TAG;
                    radlLevel.Items.FindByValue(model.SERVICE_LEVEL.ToString()).Selected = true;
                    radlServiceType.Items.FindByValue(model.TYPE_CODE).Selected = true;
                    txtServiceContent.Text = model.SERVICE_CONTENT;
                    txtSeoTitle.Text = model.SEO_TITLE;
                    txtSeoKeywords.Text = model.SEO_KEYWORDS;
                    txtSeoDescription.Text = model.SEO_DESCRIPTION;
                    txtSort.Text = model.SORT.ToString();
                    txtDesc.Text = model.SERVICE_INTRO;
                    ViewState["state"] = "update";
                    ViewState["ID"] = id;
                    Reset1.Value = "返回";
                    Reset1.Attributes.Add("onclick", "location.href='CompanyService.aspx?id=" + Request.QueryString["id"] + "&typeid=" + Request.QueryString["typeid"] + "&typeName=" + Request.QueryString["typeName"] + "'");
                }
            }
            else if (e.CommandName == "imgbtn1")
            {
                string id = e.CommandArgument.ToString();
                NJL.Model.NJL_COMPANY_SERVICE model = bll.GetModel(id);
                if (model != null)
                {
                    if (model.AUDIT_STATE == 1)
                    {
                        model.AUDIT_STATE = 0;
                    }
                    else
                    {
                        model.AUDIT_STATE = 1;
                    }
                    model.AUDIT_USER_ID = USER_ID;//审核人ID
                    model.AUDIT_USER_NAME = USER_NAME; //审核人姓名
                    model.AUDIT_DATE_TIME = DateTime.Now; //审核时间
                }
                if (bll.Update(model))
                {
                    Royalsoft.Common.MessageBox.Show(this, "审核成功");
                    bind();
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "审核失败");
                }
            }
        }
        protected void rptUserList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Literal lit = e.Item.FindControl("litNum") as Literal;
                lit.Text = (e.Item.ItemIndex + 1).ToString();
                ImageButton imgbtn = e.Item.FindControl("imgbtn1") as ImageButton;
                if (imgbtn.AlternateText == "1")
                {
                    imgbtn.ImageUrl = "~/Images/true.gif";
                }
                else if (imgbtn.AlternateText == "0")
                {
                    imgbtn.ImageUrl = "~/Images/false.gif";
                }
                DataRowView row = (DataRowView)e.Item.DataItem;
                string id = row["IMAGE_CODE"].ToString();
                string imgurl = GetImageUrl(id);
                Image img = e.Item.FindControl("Image1") as Image;
                ViewState["iamgeid"] = "";
                if (imgurl != "")
                {
                    img.ImageUrl = imgurl;
                    ViewState["iamgeid"] = imgurl;
                }
                else
                {
                    img.Visible = false;
                }
            }
        }
        protected void btnDel_Click1(object sender, EventArgs e)
        {
            string[] cbxid = HSelectId.Value.Split(',');
            foreach (string id in cbxid)
            {
                model = bll.GetModel(id);
                if (model != null)
                {
                    DeleteImageByCode(model.IMAGE_CODE);   //删除文件
                }
                if (bll.Delete(id))
                {
                    bind();
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "删除失败");
                }
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (ViewState["state"].ToString() == "add")
            {
                model.ID = DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random().Next(100, 999).ToString();
                if (FileUpload1.HasFile)
                {
                    model.IMAGE_CODE = SaveUploadFile(Request.Files);
                }
                model.NJL_COMPANY_ID = Request.QueryString["id"].ToString();
                model.SERVICE_NAME = txtServiceName.Text;
                model.TAG = txtServiceTag.Text;
                model.SERVICE_LEVEL = int.Parse(radlLevel.SelectedValue); //服务等级
                model.TYPE_CODE = radlServiceType.SelectedValue;          //服务类型
                model.SERVICE_CONTENT = txtServiceContent.Text;
                model.SEO_TITLE = txtSeoTitle.Text;
                model.SEO_KEYWORDS = txtSeoKeywords.Text;
                model.SEO_DESCRIPTION = txtSeoDescription.Text;
                model.CREATE_DATE_TIME = DateTime.Now;
                model.AUDIT_STATE = 0;   //审核状态
                model.SORT = int.Parse(txtSort.Text.Trim());
                model.SERVICE_INTRO = txtDesc.Text.Trim();
                if (bll.Add(model))
                {
                    #region 标签处理
                    NJL.BLL.NJL_CS_TAG bllTag = new BLL.NJL_CS_TAG();
                    NJL.Model.NJL_CS_TAG modelTag = new Model.NJL_CS_TAG();
                    char[] tagtxt1 = { ',', '，' };
                    string[] tag = txtServiceTag.Text.Trim().Split(tagtxt1);
                    foreach (string strtag in tag)
                    {
                        DataSet ds = bllTag.GetList("TAG='" + strtag + "' and NJL_SYS_BASE_ID='" + WEBSITE_CODE + "'");
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            modelTag = bllTag.GetModel(ds.Tables[0].Rows[0][0].ToString());
                            if (modelTag != null)
                            {
                                if (modelTag.USED_COUNT != null)
                                {
                                    modelTag.USED_COUNT += 1;
                                    if (bllTag.Update(modelTag))
                                    {
                                    }
                                }
                            }
                        }
                        else
                        {
                            if (strtag != "")
                            {
                                modelTag.ID = DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random(Guid.NewGuid().GetHashCode()).Next(100, 999).ToString();
                                modelTag.IS_SYS_TAG = 0;
                                modelTag.TAG = strtag;
                                modelTag.CREATE_DATE_TIME = DateTime.Now;
                                modelTag.NJL_SYS_BASE_ID = modelCompany.NJL_SYS_BASE_ID;
                                modelTag.SEARCH_COUNT = 0;
                                modelTag.TAG_TYPE = "5";  //服务5
                                modelTag.USED_COUNT = 0;
                                modelTag.AREA_CODE = modelCompany.AREA_CODE;
                                bllTag.Add(modelTag);
                            }
                        }
                    }
                    #endregion
                    Royalsoft.Common.MessageBox.ShowAndRedirect(this, "添加成功", "CompanyService.aspx?id=" + Request.QueryString["id"] + "&typeid=" + Request.QueryString["typeid"] + "&typeName=" + Request.QueryString["typeName"]);

                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "添加失败");
                }
            }
            else
            {
                model = bll.GetModel(ViewState["ID"].ToString());
                model.TYPE_CODE = radlServiceType.SelectedValue;
                model.SERVICE_NAME = txtServiceName.Text;
                model.TAG = txtServiceTag.Text;
                model.SERVICE_LEVEL = int.Parse(radlLevel.SelectedValue);
                model.SERVICE_CONTENT = txtServiceContent.Text;
                model.SEO_TITLE = txtSeoTitle.Text;
                model.SEO_KEYWORDS = txtSeoKeywords.Text;
                model.SEO_DESCRIPTION = txtSeoDescription.Text;
                model.UPDATE_DATE_TIME = DateTime.Now;
                model.SORT = int.Parse(txtSort.Text.Trim());
                model.SERVICE_INTRO = txtDesc.Text.Trim();
                if (bll.Update(model))
                {
                    #region 标签处理
                    NJL.BLL.NJL_CS_TAG bllTag = new BLL.NJL_CS_TAG();
                    NJL.Model.NJL_CS_TAG modelTag = new Model.NJL_CS_TAG();
                    char[] tagtxt1 = { ',', '，' };
                    string[] tag = txtServiceTag.Text.Trim().Split(tagtxt1);
                    foreach (string strtag in tag)
                    {
                        DataSet ds = bllTag.GetList("TAG='" + strtag + "' and NJL_SYS_BASE_ID='" + WEBSITE_CODE + "'");
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            modelTag = bllTag.GetModel(ds.Tables[0].Rows[0][0].ToString());
                            if (modelTag != null)
                            {
                                if (modelTag.USED_COUNT != null)
                                {
                                    modelTag.USED_COUNT += 1;
                                    if (bllTag.Update(modelTag))
                                    {
                                    }
                                }
                            }
                        }
                        else
                        {
                            if (strtag != "")
                            {
                                modelTag.ID = DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random(Guid.NewGuid().GetHashCode()).Next(100, 999).ToString();
                                modelTag.IS_SYS_TAG = 0;
                                modelTag.TAG = strtag;
                                modelTag.CREATE_DATE_TIME = DateTime.Now;
                                modelTag.NJL_SYS_BASE_ID = modelCompany.NJL_SYS_BASE_ID;
                                modelTag.SEARCH_COUNT = 0;
                                modelTag.TAG_TYPE = "5";  //服务5
                                modelTag.USED_COUNT = 0;
                                modelTag.AREA_CODE = modelCompany.AREA_CODE;
                                bllTag.Add(modelTag);
                            }
                        }
                    }
                    #endregion
                    Royalsoft.Common.MessageBox.ShowAndRedirect(this, "更新成功", "CompanyService.aspx?id=" + Request.QueryString["id"] + "&typeid=" + Request.QueryString["typeid"] + "&typeName=" + Request.QueryString["typeName"]);
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "更新失败");
                }
            }
        }
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                model = bll.GetModel(ViewState["ID"].ToString());
                if (model != null)
                {

                    DeleteImageByCode(model.IMAGE_CODE);   //删除文件

                    model.IMAGE_CODE = SaveUploadFile(Request.Files);
                    if (bll.Update(model))
                    {
                        Royalsoft.Common.MessageBox.Show(this, "上传成功");
                    }
                }
            }
            bind();
        }
    }
}